<template>
	<el-main class="bg table_wrap">
		<el-form label-position="right" :model="form" class="form p_4" label-width="120">
			<el-row>
				<el-col :xs="24" :sm="24" :lg="12">
					<el-form-item label="留言标题">
						<el-input v-model="form.title" disabled></el-input>
					</el-form-item>
				</el-col>

				<el-col :xs="24" :sm="24" :lg="12">
					<el-form-item label="邮箱">
						<el-input v-model="form.email" disabled></el-input>
					</el-form-item>
				</el-col>

				<el-col :xs="24" :sm="24" :lg="24">
					<el-form-item label="留言内容">
						<el-input type="textarea" v-model="form.content" :rows="6" disabled></el-input>
					</el-form-item>
				</el-col>

				<el-col :xs="24" :sm="24" :lg="12">
					<el-form-item label="处理状态">
						<el-tag :type="form.is_processed == 1 ? 'success' : 'warning'" size="medium">
							{{ form.is_processed == 1 ? '已处理' : '未处理' }}
						</el-tag>
					</el-form-item>
				</el-col>

				<el-col :xs="24" :sm="24" :lg="12">
					<el-form-item label="创建时间">
						<el-input :value="formatTime(form.create_time)" disabled></el-input>
					</el-form-item>
				</el-col>

				<el-col :xs="24" :sm="24" :lg="24">
					<el-form-item label="处理备注">
						<el-input type="textarea" v-model="form.process_remark" :rows="3" placeholder="请输入处理备注"></el-input>
					</el-form-item>
				</el-col>

				<el-col :xs="24" :sm="24" :lg="24">
					<el-form-item>
						<el-button v-if="form.is_processed == 0" type="success" @click="markAsProcessed()">标记为已处理</el-button>
						<el-button type="primary" @click="save()">保存</el-button>
						<el-button @click="cancel()">返回</el-button>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				// 获取数据地址
				url_get_obj: "~/api/feedback/get_obj?",
				url_set: "~/api/feedback/set?",

				// 字段ID
				field:"feedback_id",

				// 查询
				query: {
					feedback_id: 0,
				},

				// 表单
				form: {
					feedback_id: 0,
					title: '',
					content: '',
					email: '',
					is_processed: 0,
					process_remark: '',
					create_time: '',
				},
			};
		},
		methods: {
			/**
			 * 格式化时间
			 */
			formatTime(time) {
				if (!time) return '';
				
				// 如果已经是格式化的字符串，直接返回
				if (typeof time === 'string' && /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}$/.test(time)) {
					return time;
				}
				
				// 尝试使用 $toTime 格式化
				try {
					var formatted = this.$toTime(time, 'yyyy-MM-dd hh:mm:ss');
					if (formatted && formatted !== 'NaN-aN-aN aN:aN:aN') {
						return formatted;
					}
				} catch (e) {
					console.error('时间格式化失败:', e);
				}
				
				// 如果 $toTime 失败，尝试手动格式化
				try {
					var date;
					if (typeof time === 'string') {
						// 处理包含T的时间字符串
						var timeStr = time.replace('T', ' ').replace('Z', '').replace(/-/g, '/');
						date = new Date(timeStr);
					} else if (typeof time === 'number') {
						date = new Date(time);
					} else {
						date = new Date(time);
					}
					
					if (date && !isNaN(date.getTime())) {
						return date.toStr('yyyy-MM-dd hh:mm:ss');
					}
				} catch (e) {
					console.error('手动时间格式化失败:', e);
				}
				
				return time; // 如果都失败，返回原始值
			},
			
			/**
			 * 获取对象后处理
			 */
			get_obj_after(json) {
				if (json && json.result) {
					var obj = json.result.obj || json.result.list?.[0] || json.result;
					if (obj) {
						// 处理时间格式
						if (obj.create_time) {
							// 如果时间包含T，转换为标准格式
							if (obj.create_time.indexOf && obj.create_time.indexOf('T') !== -1) {
								var date = new Date(obj.create_time);
								if (date && !isNaN(date.getTime())) {
									obj.create_time = date.toStr('yyyy-MM-dd hh:mm:ss');
								}
							}
						}
						this.form = Object.assign({}, obj);
					}
				}
			},
			
			/**
			 * 标记为已处理
			 */
			markAsProcessed() {
				var _this = this;
				this.$confirm('确定要将此反馈标记为已处理吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					_this.form.is_processed = 1;
					if (!_this.form.process_remark) {
						_this.form.process_remark = '已处理';
					}
					_this.save();
				}).catch(() => {
					// 取消操作
				});
			},
			
			/**
			 * 保存
			 */
			save() {
				var _this = this;
				if (!this.form[this.field] || this.form[this.field] === 0) {
					this.$message.error('缺少反馈ID');
					return;
				}
				
				// 构建保存的数据，只保存需要更新的字段
				var saveData = {
					is_processed: this.form.is_processed || 0,
					process_remark: this.form.process_remark || ''
				};
				
				// 使用 mixin 的 set 方法
				var query = {};
				query[this.field] = this.form[this.field];
				
				this.set(saveData, query, function(res) {
					if (res && res.result) {
						// 保存成功后刷新数据
						_this.get_obj();
					}
				});
			},
			
			/**
			 * 返回
			 */
			cancel() {
				this.$router.go(-1);
			}
		},
		mounted() {
			this.get_obj();
		}
	};
</script>

